package hos.task;

import android.util.Pair;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import hos.core.AppCompat;

/**
 * <p>Title: PagingTask </p>
 * <p>Description: 任务创建 </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023-02-03 10:47
 */
public class PagingTask<T> implements Auto.Refresh, Auto.AutoRefresh, Auto.AutoLoadMore,
        Auto.AutoRefreshThread, Auto.AutoLoadMoreThread,
        Auto.AutoRefreshMap, Auto.AutoLoadMoreMap,
        Auto.AutoRefreshMapThread, Auto.AutoLoadMoreMapThread,
        Auto.AutoRefreshPair, Auto.AutoLoadMorePair,
        Auto.AutoRefreshPairThread, Auto.AutoLoadMorePairThread {
    private final PagingRefresh<T> pagingRefresh;

    public PagingTask(Auto.AutoPaging<T> autoPaging) {
        this(new PagingRefresh<>(autoPaging));
    }

    public PagingTask(PagingRefresh<T> pagingRefresh) {
        this.pagingRefresh = pagingRefresh;
        // 初始化
        AppCompat appCompat = AppCompat.getInstance();
        int startIndex = appCompat.getStartIndex();
        int pageSize = appCompat.getPageSize();
        init(startIndex, pageSize);
        setCommonParam(appCompat.getCommonParamList());
        setCommonParam(appCompat.getCommonParamMap());
    }

    /**
     * 初始化
     *
     * @param startIndex 开始下标
     * @param pageSize   每页数量
     */
    public void init(int startIndex, int pageSize) {
        pagingRefresh.setStartIndex(startIndex);
        pagingRefresh.setPageIndex(startIndex);
        pagingRefresh.setPageSize(pageSize);
    }

    /**
     * 设置通用参数
     *
     * @param obj 可变参数
     */
    public void setCommonParam(Object... obj) {
        if (obj != null) {
            pagingRefresh.setCommonParamList(Arrays.asList(obj));
        }
    }

    /**
     * 设置通用参数
     */
    public void setCommonParam(Map<String, Object> map) {
        if (map != null) {
            pagingRefresh.setCommonParamMap(map);
        }
    }

    /**
     * 设置通用参数
     */
    @SafeVarargs
    public final void setCommonParam(Pair<String, Object>... pairs) {
        if (pairs != null) {
            pagingRefresh.setCommonParamMap(pairToMap(pairs));
        }
    }

    private void execute(boolean isThread, Object... obj) {
        pagingRefresh.setThread(isThread);
        if (obj != null) {
            pagingRefresh.setParamList(Arrays.asList(obj));
        }
        refresh();
    }

    private void execute(boolean isThread, Map<String, Object> map) {
        pagingRefresh.setThread(isThread);
        if (map != null) {
            pagingRefresh.setParamMap(map);
        }
        refresh();
    }

    @Override
    public void autoRefresh(Object... obj) {
        pagingRefresh.resetPage();
        execute(false, obj);
    }

    @Override
    public void autoLoadMore(Object... obj) {
        pagingRefresh.addPage();
        execute(false, obj);
    }

    @Override
    public void autoRefreshThread(Object... obj) {
        pagingRefresh.resetPage();
        execute(true, obj);
    }

    @Override
    public void autoLoadMoreThread(Object... obj) {
        pagingRefresh.addPage();
        execute(true, obj);
    }

    @Override
    public void autoRefresh(Map<String, Object> map) {
        pagingRefresh.resetPage();
        execute(false, map);
    }

    @Override
    public void autoLoadMore(Map<String, Object> map) {
        pagingRefresh.addPage();
        execute(false, map);
    }

    @Override
    public void autoRefreshThread(Map<String, Object> map) {
        pagingRefresh.resetPage();
        execute(true, map);
    }

    @Override
    public void autoLoadMoreThread(Map<String, Object> map) {
        pagingRefresh.resetPage();
        execute(true, map);
    }


    @SafeVarargs
    @Override
    public final void autoRefresh(Pair<String, Object>... pairs) {
        autoRefresh(pairToMap(pairs));
    }

    @SafeVarargs
    @Override
    public final void autoLoadMore(Pair<String, Object>... pairs) {
        autoLoadMore(pairToMap(pairs));
    }

    @SafeVarargs
    @Override
    public final void autoRefreshThread(Pair<String, Object>... pairs) {
        autoRefreshThread(pairToMap(pairs));
    }

    @SafeVarargs
    @Override
    public final void autoLoadMoreThread(Pair<String, Object>... pairs) {
        autoLoadMoreThread(pairToMap(pairs));
    }

    /**
     * 直接刷新
     */
    @Override
    public void refresh() {
        pagingRefresh.refresh();
    }

    /**
     * 释放
     */
    public void dispose() {
        pagingRefresh.dispose();
    }

    @SafeVarargs
    private final Map<String, Object> pairToMap(android.util.Pair<String, Object>... pairs) {
        if (pairs == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        for (Pair<String, Object> pair : pairs) {
            map.put(pair.first, pair.second);
        }
        return map;
    }

}
